//
//  LSZAlbumSingerViewController.m
//  JKFuYinProject
//
//  Created by 九酷 on 2018/9/5.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LSZAlbumSingerViewController.h"

@interface LSZAlbumSingerViewController ()<UINavigationControllerDelegate,UITableViewDelegate,UITableViewDataSource>
//上个界面传的数据   这个界面都是专辑类的
 @property(nonatomic,strong) NSMutableArray * mutArrayData;//获取的数据
//用来装请求的原先请求的数组
@property(nonatomic,assign) NSInteger numberPage;//页数
@property (strong,nonatomic) UIView * viewTopBack;
@property(nonatomic,strong) UITableView * tableViewParticulars;
@end

@implementation LSZAlbumSingerViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 设置导航控制器的代理为self
    self.navigationController.delegate = self;
    [self.tableViewParticulars reloadData];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)getDataFrist
{
    self.numberPage = 1;
    [self fromData];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
-(void)fromData
{
    [self initWithDatWithPages:self.numberPage];
}
#pragma mark - 请求数据
-(void)initWithDatWithPages :(NSInteger)page
{
    NSString * album_id = [[NSString alloc]init];
    NSString * artist_id = [[NSString alloc]init];

    if ([self.type isEqualToString:@"2"]) {
        album_id = self.lastMg.id_id;
        artist_id = @"";
    }else{
        album_id = @"";
        artist_id = self.lastMg.id_id;
    }
    [LSZRequestResolutionDic getListOfAllTheMVTypes:@"1" album_id:album_id artist_id:artist_id key_word:@"" page:[NSString stringWithFormat:@"%ld",(long)page] :^(BOOL isOK, NSString * str, LSZListOfMVTypes * manager) {
        if (isOK == YES) {
            if (page == 1) {
                [self.mutArrayData removeAllObjects];
                [self.mutArrayData addObjectsFromArray:manager.arrayData];
                if(manager.arrayData.count == 0){
                    self.tableViewParticulars.mj_footer.hidden = YES;
                }
            }else{
                [self.mutArrayData addObjectsFromArray:manager.arrayData];
                if(manager.arrayData.count == 0){
                    [self.tableViewParticulars.mj_footer endRefreshingWithNoMoreData];
                }
            }
            [self.tableViewParticulars reloadData];
        }else{
            [self initWithtring:str];
        }
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    self.mutArrayData = [[NSMutableArray alloc]init];

    CGFloat height;
    if (ScreenWidth == 320) {
       height = 270-NAVIGATION_BAR_HEIGHT-40;
    }else if(ScreenWidth == 375){
        if (iPhoneX) {
            height = 280+24-NAVIGATION_BAR_HEIGHT-40;
        }else{
            height =  280-NAVIGATION_BAR_HEIGHT-40;
        }
    }else {
        height =  290-NAVIGATION_BAR_HEIGHT-40;
    }

    UIView *viewTop = [[UIView alloc]init];
    viewTop.frame = CGRectMake(0, 0, ScreenWidth, height+70);
    viewTop.backgroundColor = RGBColor(46, 49, 54);;
    [self.view addSubview:viewTop];

    [self setUiTop];
    [self getDataFrist];
}
#pragma mark - UI
-(void)setUiTop
{
    [self aboveIntroduction];
    [self.tableViewParticulars registerClass:[LSZNewVideoPlayerTableViewCell class] forCellReuseIdentifier:@"LSZNewVideoPlayerTableViewCell"];
    [self.tableViewParticulars registerClass:[LSZNewAlbumPlaybackInterfaceTableViewCell class] forCellReuseIdentifier:@"LSZNewAlbumPlaybackInterfaceTableViewCell"];
    __weak typeof(self) weakSelf = self;
#pragma mark - 上下拉  加载数据
    // 上拉刷新
    self.tableViewParticulars.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 进入刷新状态后会自动调用这个block
            weakSelf.numberPage ++;
            //首页音频视频歌手
            [weakSelf initWithDatWithPages:weakSelf.numberPage];
            [weakSelf.tableViewParticulars.mj_footer endRefreshing];
        });
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (ScreenWidth == 320) {
            return  270-NAVIGATION_BAR_HEIGHT-40;
        }else if(ScreenWidth == 375){
            if (iPhoneX) {
                return  280+24-NAVIGATION_BAR_HEIGHT-40;
            }else{
                return  280-NAVIGATION_BAR_HEIGHT-40;
            }
        }else {
            return  290-NAVIGATION_BAR_HEIGHT-40;
        }
    }
    else{
        CGFloat height = [LSZSpecialPackaging initWithReturnsthesizeOf:60 :5];
        return height;
    }
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return self.mutArrayData.count;
    }
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UITableViewHeaderFooterView * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewHeaderFooterView alloc]initWithReuseIdentifier:@"cell"];
    }
    return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        LSZNewAlbumPlaybackInterfaceTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZNewAlbumPlaybackInterfaceTableViewCell" forIndexPath:indexPath];
        cell.contentView.backgroundColor = RGBColor(46, 49, 54);
        CGRect rect = cell.viewTopBack.frame;
        CGFloat height;
        if (ScreenWidth == 320) {
            height =  270-NAVIGATION_BAR_HEIGHT-40;
        }else if(ScreenWidth == 375){
            if (iPhoneX) {
                 height =  280-NAVIGATION_BAR_HEIGHT-16;
            }else{
                 height =  280-NAVIGATION_BAR_HEIGHT-40;
            }
        }else {
             height =  280-NAVIGATION_BAR_HEIGHT-50;
        }
        rect.size.height = height;
        cell.viewTopBack.frame = rect;

        [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:self.lastMg.cover_url] placeholderImage:[UIImage imageNamed:@"devotion_bannerB"]];
        cell.butPreview.hidden = YES;
        cell.butCollection.hidden = YES;
        cell.butGiveLike.hidden = YES;
        if ([self.type isEqualToString:@"3"]) {
            cell.labelName.text = [NSString stringWithFormat:@"歌手:%@",self.lastMg.name];
            if (ScreenWidth == 320) {
                //5
                cell.labelName.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.albumLabel.frame)-10,  ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20, 16);
                cell.labelIntroduction.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.labelName.frame)+40, ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20,45);

            }else if(ScreenWidth == 375){
                // 6 7
                cell.labelName.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.albumLabel.frame)-10,  ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20, 18);
                cell.labelIntroduction.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.labelName.frame)+40, ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20,45);
            }else {
                //plus
                cell.labelName.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.albumLabel.frame)-10,  ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20, 20);
                cell.labelIntroduction.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.labelName.frame)+40, ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20,45);
            }
        }else{
            cell.albumLabel.text = [NSString stringWithFormat:@"专辑:%@",self.lastMg.name];
            cell.labelName.text = [NSString stringWithFormat:@"歌手:%@",self.lastMg.artist_name];
            if (ScreenWidth == 320) {
                //5
                cell.labelName.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.albumLabel.frame)+20,  ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20, 16);
            }else if(ScreenWidth == 375){
                // 6 7
                cell.labelName.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.albumLabel.frame)+20,  ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20, 18);
            }else {
                //plus
                cell.labelName.frame = CGRectMake(CGRectGetMaxX(cell.coverImageView.frame)+15+2, CGRectGetMaxY(cell.albumLabel.frame)+20,  ScreenWidth-CGRectGetMaxX(cell.coverImageView.frame)-20, 20);
            }
        }
        if ([self.lastMg.lyrics isEqualToString:@""]||self.lastMg.lyrics.length == 0) {
            cell.labelIntroduction.text = @"暂无简介";
        }else{
            cell.labelIntroduction.text = self.lastMg.lyrics;//简介
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else if(indexPath.section == 1){
        LSZNewVideoPlayerTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZNewVideoPlayerTableViewCell" forIndexPath:indexPath];
        cell.labelNumber.text =  [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
        if (self.mutArrayData.count > indexPath.row) {
            LSZListOfMVTypesMg * audioMg = nil;
            audioMg = self.mutArrayData[indexPath.row];
            cell.manager = audioMg;
            if ([self.type isEqualToString:@"2"]) {
                cell.labelAlbum.text = [NSString stringWithFormat:@"%@-%@",audioMg.album_name,audioMg.artist_name];
            }else{
                cell.labelAlbum.text = [NSString stringWithFormat:@"%@-%@",audioMg.artist_name,self.lastMg.name];
            }
            cell.labelPlayNumber.text = [LSZSpecialPackaging theNumberViewsForTheAlbumAhanges:audioMg.listens];
            CGFloat size = [LSZSpecialPackaging initWithReturnsthesizeOf:13.5 :0.5];
            CGFloat width = [LSZSpecialPackaging caculateWidthOfText:ScreenWidth-110 height:cell.labelAlbum.frame.size.height font:size-0.5 : cell.labelAlbum.text];
            CGRect rect = cell.labelAlbum.frame;
            rect.size.width = width-15;
            cell.labelAlbum.frame = rect;
            CGFloat heightLabel = [LSZSpecialPackaging initWithReturnsthesizeOf:18 :2];
            CGFloat widthLabPlayerNum = [LSZSpecialPackaging caculateWidthOfText:ScreenWidth-50-width+15-heightLabel height:cell.labelPlayNumber.frame.size.height font:size-0.5 :cell.labelPlayNumber.text];
            if (width-15+widthLabPlayerNum < ScreenWidth-110) {
                CGRect rect1 = cell.imageViewHeadset.frame;
                rect1.origin.x = CGRectGetMaxX(cell.labelAlbum.frame);
                cell.imageViewHeadset.frame = rect1;

                CGRect rect2 = cell.labelPlayNumber.frame;
                rect2.origin.x = CGRectGetMaxX(cell.imageViewHeadset.frame)+3;
                rect2.size.width = widthLabPlayerNum-15;
                cell.labelPlayNumber.frame = rect2;
                cell.imageViewHeadset.hidden = NO;
                cell.labelPlayNumber.hidden = NO;
            }else{
                cell.imageViewHeadset.hidden = YES;
                cell.labelPlayNumber.hidden = YES;
            }

        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    else{
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        return cell;
    }
}
#pragma mark - 切换歌
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return;
    }
    LSZListOfMVTypesMg * audioMg = nil;
    audioMg = self.mutArrayData[indexPath.row];
    LSZVideoPlaybackInterfaceVC * VC  = [[LSZVideoPlaybackInterfaceVC alloc]init];
    VC.lastMg = audioMg;
    audioMg.listens = [NSString stringWithFormat:@"%ld",(long)[audioMg.listens integerValue]+1];
    [self.navigationController pushViewController:VC animated:YES];
}
#pragma mark - UINavigationControllerDelegate
// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
///标签头部底部分割线
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableViewParticulars.backgroundColor = [UIColor clearColor];
}
-(void)aboveIntroduction
{
    UIView *bgview = [[UIView alloc] init];
    bgview.backgroundColor = self.tableViewParticulars.backgroundColor;
    [self.view insertSubview:bgview atIndex:0];
    [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(self.view.height/2);
    }];
    //NAVIGATION_BAR_HEIGHT
    self.viewTopBack = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,NAVIGATION_BAR_HEIGHT)];
    self.viewTopBack.backgroundColor = RGBColor(46, 49, 54);
    [self.view addSubview:self.viewTopBack];

    UIButton * buttonBack = [[UIButton alloc]initWithFrame:CGRectMake(0, 0 , 70, NAVIGATION_BAR_HEIGHT)];
    [buttonBack addTarget:self action:@selector(clickButBack) forControlEvents:UIControlEventTouchUpInside];
    [self.viewTopBack addSubview:buttonBack];

    UIImageView * imageViewReturn = [[UIImageView alloc]initWithFrame:CGRectMake(20, NAVIGATION_BAR_HEIGHT-34, 11, 20)];
    imageViewReturn.image = [UIImage imageNamed:@"home_nav_button_back"];
    [buttonBack addSubview:imageViewReturn];
    __weak typeof(self) weakSelf = self;
    [imageViewReturn tapGesture:^(UIGestureRecognizer *ges) {
        [weakSelf clickButBack];
    }];
    //标题
    UILabel * labelTitle = [[UILabel alloc]initWithFrame:CGRectMake((ScreenWidth-220)/2, NAVIGATION_BAR_HEIGHT-34, 220, 20)];
    labelTitle.font = FONT_18;
    labelTitle.textColor = [UIColor whiteColor];
    //1是专辑还是2歌手 3歌曲
    if([self.type isEqualToString:@"2"]){
        labelTitle.text = [NSString stringWithFormat:@"专辑"];
    }else{
        labelTitle.text = [NSString stringWithFormat:@"歌手"];
    }
    labelTitle.textAlignment = NSTextAlignmentCenter;
    [self.viewTopBack addSubview:labelTitle];
}
-(void)clickButBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.tableViewParticulars.contentOffset.y <= 0) {
        //        self.tableViewParticulars.bounces = NO;
        NSLog(@"禁止下拉");
    }
    else{
        if (self.tableViewParticulars.contentOffset.y >= 0){
            //            self.tableViewParticulars.bounces = YES;
            NSLog(@"允许上拉");
        }
    }
}
-(UITableView *)tableViewParticulars
{
    if (_tableViewParticulars == nil) {
        _tableViewParticulars = [[UITableView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, self.view.bounds.size.width, self.view.bounds.size.height - NAVIGATION_BAR_HEIGHT-HOME_INDICATOR_HEIGHT) style:UITableViewStylePlain];
        _tableViewParticulars.delegate = self;
        _tableViewParticulars.dataSource = self;
        _tableViewParticulars.separatorColor = divider;
        _tableViewParticulars.separatorInset = UIEdgeInsetsMake(0, 20, 0, 20);
        //添加以下代码关闭估算行高
        _tableViewParticulars.estimatedRowHeight = 0;
        _tableViewParticulars.estimatedSectionHeaderHeight = 0;
        _tableViewParticulars.estimatedSectionFooterHeight = 0;
        [self.view addSubview:_tableViewParticulars];
    }
    return _tableViewParticulars;
}

@end
